<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AdminSettingsController.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_AdminSettingsController extends Core_Controller_Action_Admin {

  public function indexAction() {
    $coreTable = Engine_Api::_()->getDbtable('pages', 'core');
		$page_id = $this->view->page_id = 
				$coreTable->select()->from($coreTable->info('name'), 'page_id')
				->where('name = ?', 'header')
				->query()
        ->fetchColumn();
    $content_id = 0;

    if(!empty($page_id)) {
     $contentTable = Engine_Api::_()->getDbtable('content', 'core');
		 $content_id = $contentTable->select()
				->from($contentTable->info('name'), 'page_id')
				->where('page_id = ?', $page_id)
				->where('type = ?', 'widget')
				->where('name = ?', 'seaocore.seaocores-lightbox')
				->query()
        ->fetchColumn();
    }
		$this->view->content_id = $content_id;
    $onactive_disabled = array('sitealbum_photolightbox_show', 'sitealbum_photo_badge',  'sitealbum_photo_specialalbum', 'sitealbum_comment_view', 'sitealbum_adtype','sitealbum_lightbox_onloadshowthumb', 'submit');
    $afteractive_disabled = array('submit_lsetting', 'environment_mode', 'include_in_package');
    include APPLICATION_PATH . '/application/modules/Sitealbum/controllers/license/license1.php';
    $this->view->isModsSupport = Engine_Api::_()->sitealbum()->isModulesSupport();
  }

  // ACTION FOR ACTIVIT FEED FILES CHANGES
  public function changesAction() {
    // For check If Album plugin Version is greather than 4.1.7 or album_id exist or not
//    if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
//      $db = Zend_Db_Table_Abstract::getDefaultAdapter();
//      $column_exist = $db->query("SHOW COLUMNS FROM `engine4_album_photos` LIKE 'album_id'")->fetch();
//      if (empty($column_exist)) {
//        $db->query("ALTER TABLE `engine4_album_photos` CHANGE COLUMN `collection_id` `album_id` int(11) unsigned NOT NULL AFTER `photo_id`;");
//      }
//    }

    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('sitealbum_admin_main', array(), 'sitealbum_admin_main_changes');
//    $enableSeaocore = Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('socialengineaddon');
//    if (!$enableSeaocore) {
//      echo $this->view->translate("you have not enable socialengineaddon plugin. please enable it.");
//      exit();
//    }

    //$this->view->form = $form = new Seaocore_Form_Admin_Changes_Settings();
//    $form->setTitle('Lightbox for Photos in Activity Feeds');
//    $form->getDecorator('Description')->setOption('escape', false);
//    $activityTextPath_Original = APPLICATION_PATH
//            . '/application/modules/Activity/views/scripts/_activityText.tpl';
//    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
//      if (!is_writable($activityTextPath_Original)) {
//        $form->addError('Target file could not be overwritten. You do not have write permission chmod -R 777 recursively to the directory "/application/modules/Activity/". Please give the recursively write permission to this directory and try again.');
//      } else {
//        include APPLICATION_PATH . '/application/modules/Sitealbum/controllers/license/license2.php';
//      }
//    }


    $this->view->showTip = 1;
//    if (is_file($activityTextPath_Original)) {
//      @chmod($activityTextPath_Original, 0777);
//      $fileData = file($activityTextPath_Original);
//      foreach ($fileData as $key => $value) {
//        $pos = strpos($value, 'if ($sitealbumEnable && Engine_Api::_()->sitealbum()->showLightBoxPhoto()):');
//        if ($pos !== false) {
//          $this->view->showTip = 0;
//          break;
//        }
//      }
//    }
  }

  public function faqAction() {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('sitealbum_admin_main', array(), 'sitealbum_admin_main_faq');
    $this->view->action = 'faq';
    $this->view->faq_type = $this->_getParam('faq_type', 'general');
  }

  public function checkAlbumPluginAction() {
    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
    if (!$this->getRequest()->isPost()) {
      // Some websites having database tables: engine4_album_params and engine4_album_features reported problem because of another album plugin that was previously installed on their site. Below code corrects that.
      $this->view->albumPluginTips = false;
      $file_path = APPLICATION_PATH . "/application/modules/Album/settings/manifest.php";
      $is_file = file_exists($file_path);
      $is_module = false;
      if ($is_file) {
        $ret = include $file_path;
        if ($ret['package']['author'] == 'Webligo Developments')
          $is_module = true;
      }
      $table_engine4_album_params_exist = $db->query("SHOW TABLES LIKE 'engine4_album_params'")->fetch();
      $table_engine4_album_features_exist = $db->query("SHOW TABLES LIKE 'engine4_album_features'")->fetch();
      if ($is_module && empty($table_engine4_album_features_exist) && empty($table_engine4_album_params_exist))
        return;
      $this->view->albumPluginTips = true;
    }else {
      $db->query("DELETE FROM `engine4_core_menuitems` WHERE `engine4_core_menuitems`.`name` = 'album_admin_main_photos' LIMIT 1;");
      $db->query("DELETE FROM `engine4_core_menuitems` WHERE `engine4_core_menuitems`.`name` = 'user_home_album' LIMIT 1;");
      $db->query("DELETE FROM `engine4_core_menuitems` WHERE `engine4_core_menuitems`.`name` = 'user_profile_album' LIMIT 1;");
      $db->query("INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
('mobi_browse_album', 'album', 'Albums', '', '{\"route\":\"album_general\",\"action\":\"browse\"}', 'mobi_browse', '', 2);");
      $db->query("ALTER TABLE `engine4_album_albums` CHANGE `type` `type` ENUM( 'wall', 'profile', 'message', 'blog' ) NULL DEFAULT NULL;");
      $column_exist = $db->query("SHOW COLUMNS FROM `engine4_album_photos` LIKE 'order'")->fetch();
      if (empty($column_exist)) {
        $db->query("ALTER TABLE `engine4_album_photos` ADD COLUMN `order` int(11) unsigned NOT NULL default '0' ;");
        $db->query("UPDATE `engine4_album_photos` SET `order` = `photo_id` ;");
      }

      $db->query("DROP TABLE IF EXISTS `engine4_album_features`;");
      $db->query("DROP TABLE IF EXISTS `engine4_album_params`;");
      $db->query("UPDATE `engine4_core_modules` SET `version` = '4.1.5' WHERE `engine4_core_modules`.`name` = 'album' LIMIT 1 ;");
      return $this->_helper->redirector->gotoRoute(array("module" => "core", "controller" => "packages"), 'admin_default', true);
    }
  }

  public function readmeAction() {
    $this->view->action = 'readme';
    $this->view->faq_type = $this->_getParam('faq_type', 'general');
  }

}

?>
